//
//  UserViewController.m
//  17DianYing
//
//  Created by 赵雄 on 15/9/23.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "UserViewController.h"
#import "UserRegistrationManager.h"
#import "MUser.h"
#import "AFNetworking.h"
#import "UserLondManager+Fun.h"


@interface UserViewController ()
@property (weak, nonatomic) IBOutlet UIButton *tupSeedButton;
@property (weak, nonatomic) IBOutlet UITextField *phoneID;
@property (weak, nonatomic) IBOutlet UITextField *identifyingCode;
@property (weak, nonatomic) IBOutlet UITextField *Password;
@property (weak, nonatomic) IBOutlet UIButton *topseedIdentifyingButton;


@property(nonatomic,strong)UserRegistrationManager *registrationM;
@property(nonatomic,assign)int a;

@end

@implementation UserViewController
{
    NSTimer * timer;
    int count;
    NSString * code1;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.topseedIdentifyingButton.layer.cornerRadius  = 8;
    self.topseedIdentifyingButton.layer.masksToBounds = YES;
    self.registrationM = [UserRegistrationManager share];
    [self.phoneID becomeFirstResponder];
    self.a = 1;
    self.Password.secureTextEntry = YES;
}
- (IBAction)Sendverificationcode:(id)sender {
    if ([self isRightTEL:self.phoneID.text])
    {
        //发送信息
        timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(jishi) userInfo:nil repeats:YES];
        count=30;
        
        
        [self.registrationM RegistrationWithPhonID:self.phoneID.text BackBlock:^(id date)
         {
             NSString *str = [NSString stringWithFormat:@"%@",date[@"status"]];
             
             if ([str isEqualToString:@"0"])
             {
                 [MUser alertViewShowTitle:@"提示" andMessage:@"验证码将会发送到您的手机上，\n请注意查收" andDelegate:self controltag:1000];
             }
             else if ([str isEqualToString:@"40002"])
             {
                 [MUser alertViewShowTitle:@"提示" andMessage:@"手机号儿已被注册" andDelegate:self controltag:1000];
             }
             else
             {
                 [MUser alertViewShowTitle:@"提示" andMessage:@"验证码获取失败，请重试" andDelegate:self controltag:1000];
             }
             
         }];
    }
    else
    {
        [MUser alertViewShowTitle:@"提示" andMessage:@"请输入正确手机号" andDelegate:self controltag:2];
    }

}
- (IBAction)topRegistered:(id)sender {
    
    
    if ([self isRightTEL:self.phoneID.text])
    {
        if (![self.Password.text isEqualToString:@""])
        {
            if (![self.identifyingCode.text isEqualToString:@""])
            {

                //将上述数据全部存储到NSUserDefaults中
                NSUserDefaults *userSina = [NSUserDefaults standardUserDefaults];
                
                //存储时，除NSNumber类型使用对应的类型意外，其他的都是使用setObject:forKey:
                NSString *username = [userSina objectForKey:@"username"];
                NSString *uid = [userSina objectForKey:@"uid"];
                NSString *urltow = [userSina objectForKey:@"url"];
                NSString *platformName = [userSina objectForKey:@"platformName"];
                NSLog(@"%@",uid);
                NSString *md5PassWord = [UserLondManager md5:self.Password.text];
                
                
                //我们使用AFHTTPRequestSerializer的POST提交方式
                AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
                manager.responseSerializer = [AFHTTPResponseSerializer serializer];
                NSString * url =[http stringByAppendingString:@"/mobile/thirdparty/register"];
                NSDictionary *dic = @{@"uid":uid, @"nickName":username,@"picUrl":urltow,@"phoneNum":self.phoneID.text,@"md5Password":md5PassWord,@"code":self.identifyingCode.text,@"thirdPartyType":platformName};
                NSLog(@"%@",dic);
                [manager POST:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
                    
                    id obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
                    NSDictionary *dic = obj;
                    
//                    UIStoryboard *s = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
//                    UIViewController *vc = [s instantiateViewControllerWithIdentifier:@"Homepage"];
//                    [self presentViewController:vc animated:YES completion:nil];
                    
                    NSLog(@"+++++++++++++0++++++++++%@",dic);
                    NSString *str = [NSString stringWithFormat:@"%@",dic[@"status"]];
                
                    if ([str isEqualToString:@"40016"])
                    {
                        UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"第三方注册新用户，手机新用户" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                        [a show];
                        
                        UIStoryboard *s = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
                        UIViewController *vc = [s instantiateViewControllerWithIdentifier:@"Homepage"];
                        [self presentViewController:vc animated:YES completion:nil];
                    }else if ([str isEqualToString:@"40015"]){
                        UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"第三方信息合并手机" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                        [a show];
                        
                        UIStoryboard *s = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
                        UIViewController *vc = [s instantiateViewControllerWithIdentifier:@"Homepage"];
                        [self presentViewController:vc animated:YES completion:nil];
                    }
                    else if ([str isEqualToString:@"40001"])
                    {
                        [MUser alertViewShowTitle:@"提示" andMessage:@"手机验证码错误" andDelegate:self controltag:1000];
                    }
                    else
                    {
                        UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"注册失败" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                        [a show];
                    }
                } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                    NSLog(@"_________________________%@",error.localizedDescription);
                }];
//                }];
            }
            else
            {
                [MUser alertViewShowTitle:@"提示" andMessage:@"请输入验证码" andDelegate:self controltag:1000];
            }
        }
        else
        {
            [MUser alertViewShowTitle:@"提示" andMessage:@"请输入密码" andDelegate:self controltag:1000];
        }
    }
    else
    {
        [MUser alertViewShowTitle:@"提示" andMessage:@"请输入正确手机号儿" andDelegate:self controltag:2];
    }

}
- (IBAction)topBackButton:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{

    }];
//    UIStoryboard *s = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
//    UIViewController *vc = [s instantiateViewControllerWithIdentifier:@"Enteruser"];
//    [self presentViewController:vc animated:YES completion:nil];
//    NSString *str = @"yes";
//    [vc setValue:str forKey:@"isRun"];
}

-(BOOL)isRightTEL:(NSString *)tel
{
    if (tel.length==11) {
        return YES;
    }
    return NO;
}
-(void)jishi
{
    if (count==0)
    {
        [timer invalidate];
        self.topseedIdentifyingButton.userInteractionEnabled=YES;
        self.topseedIdentifyingButton.backgroundColor=[UIColor whiteColor];
        [self.topseedIdentifyingButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
    else{
        count--;
        self.topseedIdentifyingButton.userInteractionEnabled=NO;
        self.topseedIdentifyingButton.backgroundColor=[UIColor grayColor];
        [self.topseedIdentifyingButton setTitle:[NSString stringWithFormat:@"重新获取(%d)",count] forState:UIControlStateNormal];
    }
}
- (IBAction)topEyeButton:(id)sender {
    if (self.a == 1)
    {
        self.Password.secureTextEntry=NO;
        self.a--;
    }
    else
    {
        self.Password.secureTextEntry=YES;
        self.a++;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
